﻿
function AutomateDisplay(display, startId, endId, culture, startHint, endHint) {
    startIdString = '#' + startId;
    endIdString = '#' + endId;

    if (display == "DateAndTime") { AutomateDateAndTimeDisplay(startIdString, endIdString, culture, startHint, endHint); }
    if (display == "DateOnly") { AutomateDateOnlyDisplay(startIdString, endIdString, culture, startHint, endHint); }
    if (display == "TimeOnly") { AutomateTimeOnlyDisplay(startIdString, endIdString, culture, startHint, endHint); }
    if (display == "SingleDateAndTime") { AutomateSingleDateAndTimeDisplay(startIdString, endIdString, culture, startHint, endHint); }
}

// Process the automation of start and end boxes for the "DateAndTime" display choice
function AutomateDateAndTimeDisplay(startId, endId, culture, startHint, endHint) {
    $(startId).datetimepicker({ 
        onClose: function (dateText, inst) {
            var endDateTextBox = $(endId);
            if (endDateTextBox.val() != '' && endDateTextBox.val() != endHint) {
                var testStartDate = new Date(dateText);
                var testEndDate = new Date(endDateTextBox.val());
                if (testStartDate > testEndDate) {
                    endDateTextBox.val(dateText);
                    endDateTextBox.removeClass("hinted");
                }
            }
            else {
                endDateTextBox.val(dateText);
                endDateTextBox.removeClass("hinted");
            }
        },
        onSelect: function (selectedDateTime) {
            var start = $(this).datetimepicker('getDate');
            $(endId).datetimepicker('option', 'minDate', new Date(start.getTime()));
        }
    });
    $(endId).datetimepicker({
        onClose: function (dateText, inst) {
            var startDateTextBox = $(startId);
            if (startDateTextBox.val() != '' && startDateTextBox.val() != startHint) {
                var testStartDate = new Date(startDateTextBox.val());
                var testEndDate = new Date(dateText);
                if (testStartDate > testEndDate) {
                    startDateTextBox.val(dateText);
                    startDateTextBox.removeClass("hinted");
                }
            }
            else {
                startDateTextBox.val(dateText);
                startDateTextBox.removeClass("hinted");
            }
        },
        onSelect: function (selectedDateTime) {
            var end = $(this).datetimepicker('getDate');
            $(startId).datetimepicker('option', 'maxDate', new Date(end.getTime()));
        }
    });
}


// Process the automation of start and end boxes for the "DateOnly" display choice
function AutomateDateOnlyDisplay(startId, endId, culture, startHint, endHint) {
    $(startId).datepicker({
        onClose: function (dateText, inst) {
            var endDateTextBox = $(endId);
            if (endDateTextBox.val() != '' && endDateTextBox.val() != endHint) {
                var testStartDate = new Date(dateText);
                var testEndDate = new Date(endDateTextBox.val());
                if (testStartDate > testEndDate) {
                    endDateTextBox.val(dateText);
                    endDateTextBox.removeClass("hinted");
                }
            }
            else {
                endDateTextBox.val(dateText);
                endDateTextBox.removeClass("hinted");
            }
        },
        onSelect: function (selectedDateTime) {
            var start = $(this).datepicker('getDate');
            $(endId).datepicker('option', 'minDate', new Date(start.getTime()));
        }
    });
    $(endId).datepicker({
        onClose: function (dateText, inst) {
            var startDateTextBox = $(startId);
            if (startDateTextBox.val() != '' && startDateTextBox.val() != startHint) {
                var testStartDate = new Date(startDateTextBox.val());
                var testEndDate = new Date(dateText);
                if (testStartDate > testEndDate) {
                    startDateTextBox.val(dateText);
                    startDateTextBox.removeClass("hinted");
                }
            }
            else {
                startDateTextBox.val(dateText);
                startDateTextBox.removeClass("hinted");
            }
        },
        onSelect: function (selectedDateTime) {
            var end = $(this).datepicker('getDate');
            $(startId).datepicker('option', 'maxDate', new Date(end.getTime()));
        }
    });
}

// Process the automation of start and end boxes for the "TimeOnly" display choice
function AutomateTimeOnlyDisplay(startId, endId, culture, startHint, endHint) {
    $(startId).timepicker({
        onClose: function (dateText, inst) {
            var endDateTextBox = $(endId);
            if (endDateTextBox.val() != '' && endDateTextBox.val() != endHint) {
                var now = new Date();
                var testStartDate = new Date(now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getDate() + " " + dateText);
                var testEndDate = new Date(now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getDate() + " " + endDateTextBox.val());
                if (testStartDate > testEndDate) {
                    endDateTextBox.val(dateText);
                    endDateTextBox.removeClass("hinted");
                }
            }
            else {
                endDateTextBox.val(dateText);
                endDateTextBox.removeClass("hinted");
            }
        },
        onSelect: function (selectedDateTime) {
            var end = $(this).timepicker('getTime');
            $(endId).timepicker('option', 'minTime', new Date(end.getTime()));
        }
    });
    $(endId).timepicker({
        onClose: function (dateText, inst) {
            var startDateTextBox = $(startId);
            if (startDateTextBox.val() != '' && startDateTextBox.val() != startHint) {
                var now = new Date();
                var testStartDate = new Date(now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getDate() + " " + startDateTextBox.val());
                var testEndDate = new Date(now.getFullYear() + "/" + (now.getMonth() + 1) + "/" + now.getDate() + " " + dateText);
                if (testStartDate > testEndDate) {
                    startDateTextBox.val(dateText);
                    startDateTextBox.removeClass("hinted");
                }
            }
            else {
                startDateTextBox.val(dateText);
                startDateTextBox.removeClass("hinted");
            }
        },
        onSelect: function (selectedDateTime) {
            var start = $(this).timepicker('getTime');
            $(startId).timepicker('option', 'maxTime', new Date(start.getTime()));
        }
    });
}

// Process the automation of start and end boxes for the "SingleDateAndTime" display choice
function AutomateSingleDateAndTimeDisplay(startId, endId, culture, startHint, endHint) {
    $(startId).datetimepicker({
        onClose: function (dateText, inst) {
            var endDateTextBox = $(endId);
            if (endDateTextBox.val() != '' && endDateTextBox.val() != endHint) {
                var testStartDate = new Date(dateText);
                var testEndDate = new Date(testStartDate.getFullYear() + "/" + (testStartDate.getMonth() + 1) + "/" + testStartDate.getDate() + " " + endDateTextBox.val());
                if (testStartDate > testEndDate) {
                    endDateTextBox.val($.datepicker.formatTime("hh:mm tt", $.datepicker._get(inst, 'timepicker'), ""));
                    endDateTextBox.removeClass("hinted");
                }
            }
            else {
                endDateTextBox.val($.datepicker.formatTime("hh:mm tt", $.datepicker._get(inst, 'timepicker'), ""));
                endDateTextBox.removeClass("hinted");
            }
            $(this).removeClass("hinted");
        }
    });
    $(endId).timepicker({});
}